<template>
  <div class="container">
    <el-dialog
      :title="dialog.dialogType==='edit'?'修改商超区域':'创建商超区域'"
      :visible.sync="dialog.centerDialogVisible"
      width="30%"
      center
      :lock-scroll="true"
      @close="hideDialog"
    >
      <el-form
        ref="formData"
        :rules="rules"
        :model="regions"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="区域：" prop="buyername">
          <el-input v-model="regions.name" placeholder="请输入商超区域" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getaddRegion } from "@/api/buyerRegion/region";
import { geteditRegion } from "@/api/buyerRegion/region";
import store from "@/store";
export default {
  props: {
    dialog: Object,
    regions: Object
  },
  data() {
    return {
      rules: {
        name: [{ required: true, message: "商超区域不能为空", trigger: "blur" }]
      }
    };
  },

  methods: {
    hideDialog() {
      this.dialog.centerDialogVisible = false;
      this.$refs["formData"].resetFields();
    },

    //提交
    confirm() {
      this.$refs["formData"].validate(valid => {
        if (valid) {
          let data = {
            name: this.regions.name
          };
          if (this.dialog.dialogType == "edit") {
            data.id = this.regions.id;
            geteditRegion(data)
              .then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: res.msg,
                    type: "success"
                  });
                  this.hideDialog();
                  this.$emit("upData");
                } else {
                  this.$message({
                    message: res.msg,
                    type: "error"
                  });
                }
              })
              .catch(err => {
                console.log(err);
              });
          } else {
            getaddRegion(data)
              .then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: res.msg,
                    type: "success"
                  });
                  this.hideDialog();
                  this.$emit("upData");
                } else {
                  this.$message({
                    message: res.msg,
                    type: "error"
                  });
                }
              })
              .catch(err => {
                console.log(err);
              });
          }
        }
      });
    },
    //取消
    cancel() {
      this.hideDialog();
    }
  }
};
</script>